#pragma once
#include "afxinet.h"
#include "singleton.h"

class CYPNetSssion
{
    CString m_strFormat;
    CString m_strFormat1;
    CString m_strHttpName;
    CString m_strID;
    CString GetServerInfo(CString& strHttpName, CString& strFormData);
public:
    bool operator == (const CYPNetSssion& T)
    {
        return m_strHttpName==T.m_strHttpName;
    }
    void Init(CString ip, CString id, CString format, CString format1)
    { 
        m_strFormat = format;
        m_strFormat1 = format1;
        m_strID = id;
        m_strHttpName = ip;
    }
    /*USERVERIFYRESULT*/ 
    CString VerifyUser(CString& strUserName, CString& strPSW, CString statusm, CString guid = _T(""));
    CString VerifyUser1(CString& strUserName, CString& strPSW, const CString& statusm, CString& data);
    CString VerfyPos(CString strSend);
};

class CIPDatas;

class CNetLogin :public Singleton<CNetLogin>
{
    CRITICAL_SECTION m_oCS;
    bool m_bExit;
    typedef std::vector<CYPNetSssion> VecSessions;
    VecSessions m_LinkData;
    VecSessions::iterator m_itr;
    CIPDatas* m_pData;
    HANDLE m_hThread;
    DWORD  m_dwCount;
    CString m_strGuild;
    CString m_strOK;
    std::vector<CString> m_vecHeart;
    static DWORD WINAPI Run(LPVOID lParam);
    DWORD m_dwCheckTime;
public:
    CNetLogin()
    {
        InitializeCriticalSection(&m_oCS);
        Init();
    }
    ~CNetLogin()
    {
        DeleteCriticalSection(&m_oCS);
    }
    //static CNetLogin& Instance()
    //{
    //    static CNetLogin T;
    //    return T;
    //}
    CString GetReg();
    void Init();
    bool Exit();
    bool Login(CString name, CString pwd, CString guid);
    bool Login1(CString name, CString pwd, CString guild);
    CString Heaet();
    inline const CIPDatas*IPData()
    {
        return m_pData;
    }
    inline CString GetHeart()
    {
        CString str = _T("");;
        EnterCriticalSection(&m_oCS);
        if (m_vecHeart.size())
        {
            str = m_vecHeart[0];
            //m_vecHeart.erase(m_vecHeart.begin());
        }
        LeaveCriticalSection(&m_oCS);
        return str;
    }
    inline void AddHeart(CString& str)
    {
        EnterCriticalSection(&m_oCS);
        m_vecHeart.clear();
        m_vecHeart.push_back(str);
        LeaveCriticalSection(&m_oCS);
    }
    inline void StartSend()
    {
        EnterCriticalSection(&m_oCS);
        m_dwCheckTime = GetTickCount();
        m_vecHeart.clear();
        LeaveCriticalSection(&m_oCS);
    }
    inline DWORD NeedSend()
    {
        DWORD dd;
        EnterCriticalSection(&m_oCS);
        dd = m_dwCheckTime;
        LeaveCriticalSection(&m_oCS);
        return dd;
    }
    inline void StopSend()
    {
        EnterCriticalSection(&m_oCS);
        m_dwCheckTime = 0;
        LeaveCriticalSection(&m_oCS);
    }
};